package cn.qing.service.Impl;

import cn.qing.mapper.RoleMapper;
import cn.qing.pojo.Permission;
import cn.qing.pojo.Role;
import cn.qing.service.RoleService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    RoleMapper roleMapper;

    /**
     * 查询全部角色
     * @param page
     * @param size
     * @return
     */
    @Override
    public List<Role> findAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        List<Role> roleList = roleMapper.findAll();

        return roleList;
    }

    /**
     * 添加角色
     * @param role
     */
    @Override
    public void save(Role role) {
        roleMapper.save(role);
    }

    /**
     * 查看角色详情
     * @param id
     * @return
     */
    @Override
    public Role findById(Integer id) {
        Role roles = roleMapper.findById(id);
        return roles;
    }

    /**
     * 给角色添加权限查找没有的权限
     * @param id
     * @return
     */
    @Override
    public List<Permission> findOtherPermission(Integer id) {
        return roleMapper.findOtherPermission(id);
    }

    /**
     * 为添加权限
     * @param roleId
     * @param ids
     * @return
     */
    @Override
    public void addPermissionToRole(Integer roleId, Integer[] ids) {
        for (Integer permissionId : ids){
            roleMapper.addPermissionToRole(roleId,permissionId);
        }
    }
}
